#+_+ coding:utf-8 +_+

import time

from base import BaseHdl
import auth 
from  common import errno
from common import buffer
import comment
import storage
from storage import news
from storage import comment

class PostCommentHdl(BaseHdl):
    def post(self):
        newsID = self.get_argument("news_id","0")
        uid = self.get_argument("uid","0")
        author = self.get_argument("author","")
        content = self.get_argument("comment","")
        signature = self.get_argument("signature","")

        rst = auth.IsValiedUID(uid)
        if not rst:
            rep = self.uidError()
        else:
            ticket = storage.uid.UID.GetTicket(uid)
            rst = auth.CheckSignature(signature,uid,newsID,content,author,ticket)
            if not rst :
                rep = self.signatureError()
            else:
                rst = news.CheckNewsID(newsID)
                if not rst :
                    rep  = self.newsIDError()
                else:
                    id = comment.Add(uid,newsID,author,content,time.time())
                    rep = self.success(id)
        self.write(rep)

    def newsIDError(self):
        msg = dict(errno=errno.ENEWS_ID_NOTVALIED,errmsg=errno.ErrStr(errno.ENEWS_ID_NOTVALIED))
        rep = dict(rst="FAILED",msg=msg)
        return buffer.JsonEncode(rep)

    def success(self,commentID):
        msg = dict(comment_id=commentID)
        rep = dict(rst="SUCCESS",msg=msg)
        return buffer.JsonEncode(rep)

            
